package cn.jsony.org.controller;

import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.PageList;
import cn.jsony.org.domain.Employee;
import cn.jsony.org.query.EmployeeQuery;
import cn.jsony.org.service.IEService;
import cn.jsony.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门的API",description="部门相关的CRUD功能")
@RestController//@responsebody+@controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEService service;

    //查所有-get
    @GetMapping
    @ApiOperation(value = "查询所有" )
    public List<Employee> findAll(){
        return service.findAll();
    }


    @GetMapping("/{id}")//查找一个get
    @ApiOperation(value = "通过ID查询" ) //：打在方法上，对方法做描述
    public Employee findOne(@PathVariable("id") Long id){
        return service.findOne(id);

    }
    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){

        return service.queryPage(employeeQuery);
    }

    //删除
    @ApiOperation(value = "删除" )
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            service.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    //批量删除
    @ApiOperation(value = "批量删除" )
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }




    //修改and添加
    @ApiOperation(value = "修改and添加" )
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                service.add(employee);
            }else {
                service.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    @GetMapping("/list/{id}")
    @ApiOperation(value = "根据店铺id查询所有店铺人员")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        return service.findByShopId(id);
    }
}
